package com.hainatong.jcmvvmbase.repository;

import com.hainatong.mvvmbase.http.RxSchedulersUtils;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

import java.util.concurrent.TimeUnit;

public abstract class BaseRepository
{
    private CompositeDisposable mCompositeDisposable;


    protected <T> void execute(Observable<T> observable, Observer<T> observer)
    {
        observable
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .compose(RxSchedulersUtils.Obs_io_main())
                .subscribe(observer);
    }


    public boolean isDisposed()
    {
        return  this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed();
    }

    public void addDisposable(Disposable disposable)
    {
        if (this.mCompositeDisposable == null)
        {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(disposable);
    }


    public void unDisposable()
    {
        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed())
        {
            this.mCompositeDisposable.clear();
        }
    }

}
